var util = require("../../utils/util.js")
var api = require("../../utils/api.js")
const app = getApp()
var index
Page({

  data: {
    tabArr: {
      curHdIndex: 0,
      curBdIndex: 0
    },
    page: 1,
    payWindows: false, //退款弹窗
    weChatShow: false,
    next: 0, //是否存在分页
    // orderList: '', //订单列表
    status: -1, //请求订单状态
    isLoad: true, // 拒绝原因列表
    reasonData: [{
        name: '物流太慢'
      },
      {
        name: '不想要了'
      },
      {
        name: '收货信息填错了',
      },
      {
        name: '其他',
      }
    ]
  },
  chooseReason: function(e) {
    var that = this;
    var str = e.detail.value;
    that.data.content = str
  },
  refuseTextarea: function(e) {
    this.data.refuseText = e.detail.value;
  },
  refuseSubmit: function() {
    let that = this
    if (!that.data.content) {
      util.showToast('请选择退款理由~', 'none')
      return
    }
    let refund_reason = that.data.refuseText ? `${that.data.content}~~~${that.data.refuseText}` : `${that.data.content}`

    api.exchangeApply_refund({
      data: {
        order_number: that.data.order_number,
        refund_reason: refund_reason
      },
      success: res => {
        if (res.data.code == 0) {
          let orderList = that.data.orderList
          console.log(that.data.orderList[index],'orderList')
          orderList[index].status = 7
          util.showToast('已成功申请退款', 'none')
          that.setData({
            orderList
          })
          that.popClose()
        } else {
          util.showToast(`${res.data.result.msg}`, 'none')
        }
      }
    })


  },
  tabFun: function(e) {
    //获取触发事件组件的dataset属性
    this.setData({
      isLoad: true,
      isLoading: true
    })
    var _datasetId = e.target.dataset.id;
    var _obj = {};
    _obj.curHdIndex = _datasetId;
    _obj.curBdIndex = _datasetId;
    let status
    if (_datasetId == 0) {
      status = -1
    } else if (_datasetId == 1) {
      status = 0
    } else if (_datasetId == 2) {
      status = 2
    } else {
      status = 3
    }
    this.setData({
      tabArr: _obj,
      status: status,
      page: 1,
      orderList: ''
    });
    this.getOrderList(1, status)
  },

  onLoad: function(options) {
    if (options.index) {
      let tabArr = this.data.tabArr
      tabArr.curHdIndex = Number(options.index)
      tabArr.curBdIndex = Number(options.index)
      let status
      if (options.index == 1) {
        status = 0
      } else if (options.index == 2) {
        status = 2
      } else if (options.index == 3) {
        status = 3
      }
      this.setData({
        tabArr,
        status
      })
    } else {
      let tabArr = {
        curHdIndex: 0,
        curBdIndex: 0,
      }
      this.setData({
        tabArr,
        status: -1
      })
    }
    let that = this
    that.setData({
      authStatus: app.globalData.authStatus
    })
    // if(){
    // }
    if (!app.globalData.loginStatus) {
      //使用es6的promise进行异步处理
      let promise = new Promise(function(resolve, reject) {
        util.getLogin(resolve)
      });
      promise.then(function() {

        that.getOrderList(1, that.data.status)
      })
    } else {
      that.getOrderList(1, that.data.status)
    }
  },
  onShow() {

  },
  //获取订单列表
  getOrderList: function(page, status) {
    let that = this
    api.orderList({
      data: {
        page: page,
        limit: 10,
        status: status
      },
      success: res => {
        if (res.data.result.data) {
          if (that.data.orderList) {
            let newOrderList = that.data.orderList.concat(res.data.result.data)
            that.setData({
              isLoading: false,
              orderList: newOrderList,
              page: page,
            })
          } else {
            let newOrderList = res.data.result.data
            that.setData({
              isLoading: false,
              orderList: newOrderList
            })
          }
          that.setData({
            isLoading: false,
            next: res.data.result.next,
            isLoad: false,
            authStatus: app.globalData.authStatus
          })
        }
      }
    })
  },
  //触底加载下一页
  onReachBottom: function() {
    let that = this
    if (that.data.next != 0) {
      let page = that.data.page + 1
      that.getOrderList(page, that.data.status)
    } else {
      util.showToast('没有更多数据了~', 'none')
    }

  },
  //复制快递单号
  copyExpress: function(e) {
    let that = this
    let index = e.currentTarget.dataset.index
    let orderList = that.data.orderList
    wx.setClipboardData({
      data: orderList[index].express_order,
      success: function(res) {}
    })
  },
  //复制订单编号
  copyOrder: function(e) {
    let order = e.currentTarget.dataset.order
    wx.setClipboardData({
      data: order,
      success: function(res) {}
    })
  },
  //下拉刷新
  onPullDownRefresh: function() {
    let that = this
    api.orderList({
      data: {
        page: 1,
        limit: 10,
        status: that.data.status
      },
      success: res => {
        wx.stopPullDownRefresh();
        that.setData({
          orderList: res.data.result.data,
          next: res.data.result.next
        })
      }
    })
  },
  //跳转商品详情
  getGoodsDetail: function(e) {
    let type = e.currentTarget.dataset.type
    if (type == 4) {
      wx.navigateTo({
        url: '../redPacket/redPacket?gid=' + e.currentTarget.dataset.gid,
      })
    } else {
      if (e.currentTarget.dataset.mid == 2) {
        wx.navigateTo({
          url: '../activity/activity?gid=' + e.currentTarget.dataset.gid,
        })
      } else {
        wx.navigateTo({
          url: '../commodityDetail/commodityDetail?gid=' + e.currentTarget.dataset.gid,
        })
      }
    }
  },
  // onHide: function() {
  //   this.setData({
  //     orderList: ''
  //   })
  // }
  // 确认收货按钮
  affirmGoods: function(e) {
    let that = this
    let orderList = that.data.orderList
    let index = e.currentTarget.dataset.index
    let uid = app.globalData.uid
    let formid = e.detail.formId
    let type = e.currentTarget.dataset.type || 0
    api.confirm_receipt({
      data: {
        order_number: orderList[index].order_number,
        uid: uid,
        formid: formid

      },
      success: res => {
        if (res.data.code == 0) {
          util.showToast(res.data.result.msg, 'none')
          if (type == 2) {
            orderList.splice(index, 1)
          } else {
            orderList[index].status = 3
          }
          that.setData({
            orderList
          })
        } else {
          util.showToast('系统错误', 'none')
        }
      }
    })

  },
  popClose: function(e) {
    this.closePayWindows()
    this.setData({
      payWindows: false
    })
  },
  applyTk: function(e) {
    let that = this
    let item = e.currentTarget.dataset.item
    index = e.currentTarget.dataset.index
    if (item.status == 1) {
      util.saveFromId(e.detail.formId)
      that.showPayWindows()
      that.setData({
        payWindows: true,
        order_number: item.order_number,
      })
      return
      wx.showModal({
        title: '提示',
        content: '审核人员将在1-3个工作日内审核，是否确认申请退款?',
        success(res) {
          if (res.confirm) {
            api.exchangeApply_refund({
              data: {
                order_number: item.order_number,
              },
              success: res => {
                if (res.data.code == 0) {
                  let orderList = that.data.orderList
                  orderList[index].status = 7
                  util.showToast('已成功申请退款', 'none')
                  that.setData({
                    orderList
                  })
                } else {
                  util.showToast(`${res.data.result.msg}`, 'none')
                }
              }
            })
          } else if (res.cancel) {
            util.showToast(`取消退款~`, 'none')
          }
        }
      })


    } else if (item.status == 4 || item.status == 7) {
      util.showToast('退款申请正在审核中', 'none')
    }
  },
  // 打开购买动画
  showPayWindows: function(e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
      delay: 0
    })
    that.animation = animation;
    animation.translateY(-220).step();
    that.setData({
      upMenuAction: animation.export()
    })
  },
  // 关闭购买动画
  closePayWindows: function(e) {
    var that = this;
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'linear',
      delay: 0
    })
    that.animation = animation;
    animation.translateY(220).step();
    that.setData({
      upMenuAction: animation.export()
    })
  },
  // 授权方法
  bindGetUserInfo(e) {
    let that = this
    if (e && e.detail.userInfo) {
      let userInfo = {
        signature: e.detail.signature,
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        code: app.globalData.code,
      }
      if (that.data.red_id) {
        userInfo.red_id = that.data.red_id
        that.useHb()
      }
      util.saveUserInfo(that, userInfo).then(res => {
      }).catch(err => {
          console.log(err)
      })
    } else {
      that.setData({
        authStatus: false
      })
    }
  },
});